const Router = require('koa-router');
const scriptRunner = require('../../script/scriptRunner.js');

const router = new Router();

// 玩家点击 NPC
router.post('/api/npc/talk', async (ctx) => {
    const { npcId, player } = ctx.request.body;
    console.log(`玩家 ${player.name} 点击了 NPC ${npcId}`);
    
    try {
        const result = await scriptRunner.runScript(npcId, player, 'talk');
        ctx.body = result;
    } catch (err) {
        ctx.status = 500;
        ctx.body = { error: err.message };
    }
});

// 玩家做出选择（继续执行脚本）
router.post('/api/npc/continue', async (ctx) => {
    const { npcId, player, input } = ctx.request.body;

    try {
        const result = await scriptRunner.runScript(npcId, player, 'continue', input);
        ctx.body = result;
    } catch (err) {
        ctx.status = 500;
        ctx.body = { error: err.message };
    }
});

module.exports = router;